package leaf.science.ui.camera
{

import caurina.transitions.Tweener;

import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.text.TextFieldAutoSize;
import flash.utils.Timer;

import leaf.science.Global;


public class DemoToolTip extends DemoToolTipSkin
{

	private var _closing:Boolean = false;
	private var _closeDelay:Timer;
	public var hotspot:DemoHotspot;

	public function DemoToolTip( h:DemoHotspot )
	{
//		trace( "CONSTRUCTED " + this + " " + h );
		
		var data:XML = ( Global.DATA_DEMOS..demo.( id == h.id ))[0]; 

		hotspot = h;
		
		_closeDelay = new Timer( 50 );
		_closeDelay.addEventListener( TimerEvent.TIMER, onCloseDelayComplete );
		
		this.mouseChildren = false;
		this.buttonMode = true;
		this.alpha = 0;
		this.addEventListener( MouseEvent.MOUSE_OVER, onMouseOver );
		this.addEventListener( MouseEvent.MOUSE_OUT, onMouseOut );
		
		
		this.title.autoSize = "left";
		this.title.text = data.title;
//		this.info.htmlText = StringUtils.removeExtraWhitespace( "<b>"+data.title+"</b><br><br>"+data.text );		
		this.info.htmlText = StringUtils.removeExtraWhitespace( data.text );		
	}
	
	public function open():void
	{
		if( _closeDelay.running )
			stopClosing();
		else
		{
			this.y -= 15;
			Tweener.addTween( this, { alpha:1, y:y+15, time:0.3, transition:"easeOutCubic", onComplete:onOpenComplete });
//			Tweener.addTween( this, { alpha:1, time:0.4, transition:"easeOutCubic", onComplete:onOpenComplete });
//			Tweener.addTween( this, { y:y+30, time:0.5, transition:"easeOutBounce" });
		}
	}
	
	private function onOpenComplete():void
	{
		this.info.visible = true;
	}
	
	public function close():void
	{
		_closeDelay.start();
	}
	
	public function closeNow():void
	{
		Tweener.addTween( this, { alpha:0, y:y-20, time:0.2, transition:"easeOutCubic", onComplete:onCloseComplete });
		hotspot.toolTip = null;
		this.info.visible = false;	
	}
	
	private function onCloseDelayComplete( evt:TimerEvent=null ):void
	{
		stopClosing();
		closeNow();
	}
	
	private function onCloseComplete():void
	{
		stopClosing();
		Tweener.removeTweens( this );
		if( parent ) parent.removeChild( this );
	}
	
	private function onMouseOver( evt:MouseEvent ):void
	{
		stopClosing();
	}
	
	private function onMouseOut( evt:MouseEvent ):void
	{
		close();
	}

	private function stopClosing():void
	{
		if( _closeDelay.running )
			_closeDelay.stop();
	}
	
	override public function toString():String
	{
		return( "[DemoToolTip]" );
	}
}
}